﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace DIPLib
{
    public partial class ThresholdSeekBar : UserControl
    {
        private int xBarMin = 6, xBarMax = 262;
        byte mLevel = 128; // Level = 0-> 255
        private int yBarMin = 40;
        private Bitmap mArrow;
        private bool isSeeking = false;
        public event EventHandler ValuesChanged;

        public byte Level
        {
            get
            {
                return mLevel;
            }
            set
            {
                mLevel = value;
                Invalidate();
            }
        }
        public ThresholdSeekBar()
        {
            InitializeComponent();
            mArrow = DIPLib.Properties.Resources.arrow;
            mArrow.MakeTransparent(Color.FromArgb(255, 255, 255));
            txtLevel.Text = "" + mLevel;
        }

        private void SeekBar_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Black);
            SolidBrush br = new SolidBrush(Color.Black);
            g.DrawRectangle(p, xBarMin, yBarMin, 256, 12);
            g.FillRectangle(br, xBarMin, yBarMin, mLevel + 1, 12);
            if (ValuesChanged != null)
                ValuesChanged(this, new EventArgs());
            g.DrawImage(mArrow, xBarMin + mLevel - 4, yBarMin + 13, 9, 14);

        }

        private void SeekBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y >= yBarMin + 13 && e.Y <= yBarMin + 27)
            {
                if (e.X >= xBarMin && e.X <= xBarMax)
                {
                    isSeeking = true;
                }
            }
        }

        private void SeekBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (isSeeking)
            {
                int x = e.X;
                if (x >= xBarMin && x < xBarMax)
                {
                    mLevel = (byte)(x - xBarMin);
                    txtLevel.Text = mLevel + "";
                    Invalidate();
                }
            }
        }

        private void SeekBar_MouseUp(object sender, MouseEventArgs e)
        {
            int x = e.X;
            if (e.X >= xBarMin && e.X < xBarMax)
            {
                if (e.Y >= yBarMin + 13 && e.Y <= yBarMin + 27)
                {
                    mLevel = (byte)(x - xBarMin);
                    txtLevel.Text = mLevel + "";

                    //Refresh();
                    Invalidate();
                }
            }
            isSeeking = false;
        }

        private void txtLevel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int i;
                try
                {
                    i = int.Parse(txtLevel.Text);
                    if (i >= 0 && i < 256)
                    {
                        mLevel = (byte)i;
                        Invalidate();
                        if (ValuesChanged != null)
                            ValuesChanged(this, new EventArgs());
                    }
                }
                catch (System.Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }
            }
        }
    }
}
